<template>
  <div class="mod-config">
    <basic-container>
      <el-row :gutter="24">
        <el-col :xs="24" :sm="24" :md="5" class="user__tree">
          <el-tree :data="treeData" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
        </el-col>
        <el-col :xs="24" :sm="24" :md="19">
          <el-form :inline="true" :model="dataForm">
            <el-form-item>
              <el-button v-if="permissions.eims_addfee_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading">
            <el-table-column prop="username" label="用户名"></el-table-column>
            <el-table-column prop="realName" label="姓名"></el-table-column>
            <el-table-column prop="deptId" label="所属主部门"></el-table-column>
            <el-table-column prop="phone" label="手机号"></el-table-column>
            <el-table-column prop="unitId" label="单位"></el-table-column>
            <el-table-column prop="jobType" label="岗位类型"></el-table-column>
            <el-table-column prop="posId" label="岗位"></el-table-column>
            <el-table-column prop="lockFlag" label="状态"></el-table-column>
            <el-table-column prop="createTime" label="创建时间"></el-table-column>
            <el-table-column header-align="center" align="center" label="操作">
              <template slot-scope="scope">
                <el-button v-if="permissions.eims_addfee_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">组织</el-button>
                <el-button v-if="permissions.eims_addfee_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
                <el-button v-if="permissions.eims_addfee_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="avue-crud__pagination">
            <el-pagination
                    @size-change="sizeChangeHandle"
                    @current-change="currentChangeHandle"
                    :current-page="pageIndex"
                    :page-sizes="[10, 20, 50, 100]"
                    :page-size="pageSize"
                    :total="totalPage"
                    background
                    layout="total, sizes, prev, pager, next, jumper">
            </el-pagination>
          </div>
        </el-col>
      </el-row>




      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    </basic-container>
  </div>
</template>

<script>
  import {fetchAddFeeList, delAddFeeObj} from '@/api/eims/add-fee.js'
  import {fetchSupplyList} from '@/api/eims/supply.js'
  import TableForm from './form'
  import {mapGetters} from 'vuex'

  import { fetchTree } from '@/api/admin/dept'
  import { addObj, delObj, fetchList, putObj, getDetails, updateUserOrg} from '@/api/admin/user'


  export default {
    data () {
      return {
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        dataForm: {},
        supplyOptions: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm
    },
    created () {
      this.getSupplyOptions()
      this.init()
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      init() {
        //获取树结构
        fetchTree().then(response => {
          this.treeData = response.data.data
          console.log(this.treeData)
        })
      },
      handleNodeClick(data){
        this.pageIndex = 1
        this.getList({
            deptId: data.id
        })
      },
      // 获取数据列表
      getList(params) {
        this.dataListLoading = true
        fetchList(Object.assign({ current: this.pageIndex, size: this.pageSize },params)).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
          this.dataListLoading = false
        }).catch(() => {
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delAddFeeObj(id)
        }).then(data => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      },
      // 获取制造商列表
      getSupplyOptions(){
        fetchSupplyList(Object.assign({current: 1,size: 10000})).then(result => {
          this.supplyOptions = result.data.data.records
        })
      },
      // 格式转换
      changeFormat(val){
        let obj = {}
        obj = this.supplyOptions.find(item => {
          if(item.id == val){
            return item
          }
        })
        if(obj && obj.name){
          return obj.name
        }
      }
    }
  }
</script>
